<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>vue路由钩子 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="vue路由钩子介绍 路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转   路由钩子函数总共有6个 全局的路由钩子函数：beforeEach、afterEach（比较常用） 单个的路由钩子函数：beforeEnter 组件内的路由钩子函数：beforeRouteEnter、beforeRouteLeave、beforeRouteUpdate       1.全局钩子（守卫）在src&#x2F;router">
<meta property="og:type" content="article">
<meta property="og:title" content="vue路由钩子">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/01/26/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="vue路由钩子介绍 路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转   路由钩子函数总共有6个 全局的路由钩子函数：beforeEach、afterEach（比较常用） 单个的路由钩子函数：beforeEnter 组件内的路由钩子函数：beforeRouteEnter、beforeRouteLeave、beforeRouteUpdate       1.全局钩子（守卫）在src&#x2F;router">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-25T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-26T06:35:42.785Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-vue路由钩子" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/01/26/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/" class="article-date">
  <time class="dt-published" datetime="2021-01-25T16:00:00.000Z" itemprop="datePublished">2021-01-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      vue路由钩子
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="vue路由钩子介绍"><a href="#vue路由钩子介绍" class="headerlink" title="vue路由钩子介绍"></a>vue路由钩子介绍</h2><hr>
<h3 id="路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转"><a href="#路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转" class="headerlink" title="路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转"></a>路由钩子主要作用是拦截导航，让他完成跳转或者取消跳转</h3><hr>
<br>

<h2 id="路由钩子函数总共有6个"><a href="#路由钩子函数总共有6个" class="headerlink" title="路由钩子函数总共有6个"></a>路由钩子函数总共有6个</h2><ul>
<li>全局的路由钩子函数：beforeEach、afterEach（比较常用）</li>
<li>单个的路由钩子函数：beforeEnter</li>
<li>组件内的路由钩子函数：beforeRouteEnter、beforeRouteLeave、beforeRouteUpdate  </li>
</ul>
<hr>
<br>

<h2 id="1-全局钩子（守卫）"><a href="#1-全局钩子（守卫）" class="headerlink" title="1.全局钩子（守卫）"></a>1.全局钩子（守卫）</h2><h3 id="在src-router-index-js中使用，如下："><a href="#在src-router-index-js中使用，如下：" class="headerlink" title="在src/router/index.js中使用，如下："></a>在src/router/index.js中使用，如下：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 定义路由配置</span><br><span class="line">const router &#x3D; new VueRouter(&#123; ... &#125;)</span><br><span class="line">&#x2F;&#x2F; 全局路由拦截-进入页面前执行</span><br><span class="line">router.beforeEach((to, from, next) &#x3D;&gt; &#123;</span><br><span class="line">    &#x2F;&#x2F; 这里可以加入全局登陆判断</span><br><span class="line">    &#x2F;&#x2F; 继续执行</span><br><span class="line">    next()</span><br><span class="line">   &#125;);</span><br><span class="line">    &#x2F;&#x2F; 全局后置钩子-常用于结束动画等</span><br><span class="line">    router.afterEach(() &#x3D;&gt; &#123;</span><br><span class="line">        &#x2F;&#x2F;不接受</span><br><span class="line">       next</span><br><span class="line">      &#125;)</span><br><span class="line">    export default router;</span><br></pre></td></tr></table></figure>
<h3 id="1-使用router-beforeEach注册一个全局前置钩子-（守卫）"><a href="#1-使用router-beforeEach注册一个全局前置钩子-（守卫）" class="headerlink" title="(1).使用router.beforeEach注册一个全局前置钩子 （守卫）"></a>(1).使用router.beforeEach注册一个全局前置钩子 （守卫）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const router &#x3D; new VueRouter(&#123; ... &#125;)</span><br><span class="line"></span><br><span class="line">router.beforeEach((to, from, next) &#x3D;&gt; &#123;</span><br><span class="line">    &#x2F;&#x2F; ...</span><br><span class="line">   &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="每个钩子方法接收三个参数："><a href="#每个钩子方法接收三个参数：" class="headerlink" title="每个钩子方法接收三个参数："></a>每个钩子方法接收三个参数：</h3><p>to：Route：即将要就如的目标（路由对象）,（to是一个对象，是将要进入的路由对象，可以用to.path调用路由对象中的属性）<br>from：Route：当前导航正要离开的路由<br>next：Function：继续执行函数</p>
<ul>
<li>next():继续执行</li>
<li>next(flase):中断当前的导航</li>
<li>next(‘/‘)或next({path:’/‘}):跳转新页面，常用于登录失效跳转登录</li>
<li>next(error):如果出入next的参数是一个error实例，则导航会被终止且该错误会被传递给router.onError()注册过的回调</li>
</ul>
<h2 id="2-afterEach-全局后置钩子"><a href="#2-afterEach-全局后置钩子" class="headerlink" title="(2).afterEach(全局后置钩子)"></a>(2).afterEach(全局后置钩子)</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">router.afterEach((to,form)&#x3D;&gt;&#123;</span><br><span class="line">    &#x2F;&#x2F; ……</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="和守卫不同的是，这些钩子不会接受next函数也不会改变导航本身"><a href="#和守卫不同的是，这些钩子不会接受next函数也不会改变导航本身" class="headerlink" title="和守卫不同的是，这些钩子不会接受next函数也不会改变导航本身"></a>和守卫不同的是，这些钩子不会接受next函数也不会改变导航本身</h3><hr>
<br>

<h2 id="2-单个的路由钩子（守卫）"><a href="#2-单个的路由钩子（守卫）" class="headerlink" title="2.单个的路由钩子（守卫）"></a>2.单个的路由钩子（守卫）</h2><p>在路由配置中单独加入钩子，在src/router/index.js中使用,写在路由配置中，只有访问到这个路径，才能出发钩子函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    path:&#39;&#x2F;home&#x2F;one&#39;, &#x2F;&#x2F; 子页面1</span><br><span class="line">        component: One,</span><br><span class="line">        &#x2F;&#x2F; 路由内钩子</span><br><span class="line">        beforeEnter: (to, from, next) &#x3D;&gt; &#123;</span><br><span class="line">        console.log(&#39;进入前执行&#39;);</span><br><span class="line">            next();</span><br><span class="line">        &#125;&#125;</span><br></pre></td></tr></table></figure>
<h3 id="这些守卫于全局前置守卫的方法参数是一样的"><a href="#这些守卫于全局前置守卫的方法参数是一样的" class="headerlink" title="这些守卫于全局前置守卫的方法参数是一样的"></a>这些守卫于全局前置守卫的方法参数是一样的</h3><hr>
<br>

<h2 id="3-组件内钩子（守卫）"><a href="#3-组件内钩子（守卫）" class="headerlink" title="3.组件内钩子（守卫）"></a>3.组件内钩子（守卫）</h2><h3 id="在路由组件内定义钩子函数："><a href="#在路由组件内定义钩子函数：" class="headerlink" title="在路由组件内定义钩子函数："></a>在路由组件内定义钩子函数：</h3><ul>
<li>beforeRouteEnter：进入页面前调用</li>
<li>beforeRouteUpdate：页面路由改变时调用，一般需要带参数</li>
<li>beforeRouteLeave：离开页面调用</li>
</ul>
<h3 id="写在组件中，访问路径，即将渲染组件的时候触发的"><a href="#写在组件中，访问路径，即将渲染组件的时候触发的" class="headerlink" title="写在组件中，访问路径，即将渲染组件的时候触发的"></a>写在组件中，访问路径，即将渲染组件的时候触发的</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">    name: &#39;Two&#39;,</span><br><span class="line">    data () &#123;</span><br><span class="line">        return &#123;</span><br><span class="line">            msg: &#39;Hi, I am Two Page!&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 进入页面前调用</span><br><span class="line">    beforeRouteEnter(to, from, next) &#123;</span><br><span class="line">        console.log(&#39;进入enter路由钩子&#39;)</span><br><span class="line">        next()</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 离开页面调用</span><br><span class="line">    beforeRouteLeave(to,from, next)&#123;</span><br><span class="line">        console.log(&#39;进入leave路由钩子&#39;)</span><br><span class="line">        next()</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 页面路由改变时调用</span><br><span class="line">    beforeRouteUpdate(to, from, next) &#123;</span><br><span class="line">        console.log(&#39;进入update路由钩子&#39;)</span><br><span class="line">        console.log(to.params.id)</span><br><span class="line">        next()</span><br><span class="line">    &#125;&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="beforeRouteEnter-守卫-不能-访问-this，因为守卫在导航确认前被调用-因此即将登场的新组件还没被创建。不过，你可以通过传一个回调给-next来访问组件实例。在导航被确认的时候执行回调，并且把组件实例作为回调方法的参数。"><a href="#beforeRouteEnter-守卫-不能-访问-this，因为守卫在导航确认前被调用-因此即将登场的新组件还没被创建。不过，你可以通过传一个回调给-next来访问组件实例。在导航被确认的时候执行回调，并且把组件实例作为回调方法的参数。" class="headerlink" title="beforeRouteEnter 守卫 不能 访问 this，因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建。不过，你可以通过传一个回调给 next来访问组件实例。在导航被确认的时候执行回调，并且把组件实例作为回调方法的参数。"></a>beforeRouteEnter 守卫 不能 访问 this，因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建。不过，你可以通过传一个回调给 next来访问组件实例。在导航被确认的时候执行回调，并且把组件实例作为回调方法的参数。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">beforeRouteEnter(to,from,next)&#123;</span><br><span class="line">    next(vm &#x3D;&gt;&#123;</span><br><span class="line">        &#x2F;&#x2F; 通过‘vm’访问组件实例</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<br>

<h3 id="注意-：-beforeRouteEnter-是支持给-next-传递回调的唯一守卫。对于-beforeRouteUpdate-和-beforeRouteLeave-来说，this-已经可用了，所以不支持传递回调，因为没有必要了。"><a href="#注意-：-beforeRouteEnter-是支持给-next-传递回调的唯一守卫。对于-beforeRouteUpdate-和-beforeRouteLeave-来说，this-已经可用了，所以不支持传递回调，因为没有必要了。" class="headerlink" title="注意~ ： beforeRouteEnter 是支持给 next 传递回调的唯一守卫。对于 beforeRouteUpdate 和 beforeRouteLeave 来说，this 已经可用了，所以不支持传递回调，因为没有必要了。"></a><b>注意~</b> ： beforeRouteEnter 是支持给 next 传递回调的唯一守卫。对于 beforeRouteUpdate 和 beforeRouteLeave 来说，this 已经可用了，所以不支持传递回调，因为没有必要了。</h3><br>

<h3 id="这个离开守卫beforeRouteLeave-通常用来禁止用户在还未保存修改前突然离开。该导航可以通过-next-false-来取消。"><a href="#这个离开守卫beforeRouteLeave-通常用来禁止用户在还未保存修改前突然离开。该导航可以通过-next-false-来取消。" class="headerlink" title="这个离开守卫beforeRouteLeave()通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。"></a>这个离开守卫beforeRouteLeave()通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">beforeRouteLeave(to,from,next)&#123;</span><br><span class="line">    const answer &#x3D; window.confirm(&#39;&#39;)</span><br><span class="line">    if (answer)&#123;</span><br><span class="line">        next()</span><br><span class="line">    &#125;</span><br><span class="line">    else&#123;</span><br><span class="line">        next(false)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<br>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/01/26/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/" data-id="ckkt9opg1000fi4tw2gfm8zg8" data-title="vue路由钩子" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/01/27/vue%E7%88%B6%E5%AD%90%E7%BB%84%E4%BB%B6%E3%80%81%E5%AD%90%E7%88%B6%E7%BB%84%E4%BB%B6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          vue父子组件、子父组件
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/25/vue%E6%8B%A6%E6%88%AA%E5%99%A8/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">vue拦截器!</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>